package com.littcore.saap.message.biz.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;

import com.littcore.common.BeanManager;
import com.littcore.dao.BaseJdbcDao;
import com.littcore.saap.common.vo.IUserInfo;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.message.biz.IUserMessageBizService;
import com.littcore.saap.message.bo.UserMessageBo;
import com.littcore.saap.message.dao.UserMessageDao;
import com.littcore.saap.message.po.UserMessage;
import com.littcore.saap.message.vo.UserMessageStatVo;
import com.littcore.saap.system.service.IUserInfoService;
import com.littcore.util.BeanCopier;

/**
 * .
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2013-11-19
 * @version 1.0
 */
public class UserMessageBizServiceImpl implements IUserMessageBizService {
  
  @Resource
  private UserMessageDao userMessageDao;
  @Resource
  private IUserInfoService userInfoService;
  @Resource
  private BaseJdbcDao jdbcDao;
  
  public void delete(long id)
  {
    userMessageDao.deleteById(id);
  }
  
  /* (non-Javadoc)
   * @see com.littcore.saap.message.biz.impl.IUserMessageBizService#doRead(long)
   */
  @Override
  public UserMessageBo doRead(long id)
  {
    UserMessage userMessage = userMessageDao.load(id);
    userMessage.setHasReceived(true);
    userMessage.setReceivedDatetime(new Date());
    userMessageDao.update(userMessage);
    
    UserMessageBo bo = convert(userMessage);
    return bo;
  }
    
  
  /* (non-Javadoc)
   * @see com.littcore.saap.message.biz.impl.IUserMessageBizService#countUnread(int, int)
   */
  @Override
  public List<UserMessageStatVo> countUnread(int userId, int tenantId)
  {
    String sql = "SELECT COUNT(T.ID) AS UNREAD_COUNT, MAX(T.CREATE_DATETIME) AS LAST_DATETIME, T.MODULE_CODE FROM USER_MESSAGE T WHERE T.RECEIVE_BY=? AND T.TENANT_ID=? AND T.HAS_RECEIVED=? GROUP BY T.MODULE_CODE";
    List<UserMessageStatVo> rsList = jdbcDao.getJdbcTemplate().query(sql, new RowMapper<UserMessageStatVo>() {

      @Override
      public UserMessageStatVo mapRow(ResultSet rs, int rowNum) throws SQLException
      {
        String moduleCode = rs.getString("MODULE_CODE");
        int unreadCount = rs.getInt("UNREAD_COUNT");
        Date lastDatetime = rs.getTimestamp("LAST_DATETIME");
        UserMessageStatVo vo = new UserMessageStatVo(moduleCode, unreadCount, lastDatetime);
        return vo;
      }
      
    }, new Object[]{userId, tenantId, false});
    return rsList;    
  }
  
  /* (non-Javadoc)
   * @see com.littcore.saap.message.biz.impl.IUserMessageBizService#findUnread(int, int)
   */
  @Override
  public List<UserMessageBo> findUnread(int userId, int tenantId)
  {
    String hql = "from UserMessage where receiveBy=? and tenantId=? and hasReceived=? order by createDatetime asc";
    List<UserMessage> rsList = userMessageDao.listAll(hql, new Object[]{userId, tenantId, false});
    
    List<UserMessageBo> boList = new ArrayList<UserMessageBo>(rsList.size());
    for (UserMessage userMessage : rsList)
    {
      UserMessageBo bo = convert(userMessage);
      
      boList.add(bo);
    }
    return boList;
  }


  /**
   * @param userMessage
   * @return
   */
  private UserMessageBo convert(UserMessage userMessage)
  {
    UserMessageBo bo = BeanCopier.copy(userMessage, UserMessageBo.class);
    Locale locale = LoginUtils.getLocale();
    
    IUserInfo receiveUser = userInfoService.find(bo.getReceiveBy());
    bo.setReceiveUser(receiveUser);
    
    String moduleName = BeanManager.getMessage("message.module."+bo.getModuleCode(), locale);
    bo.addI18n("moduleName", moduleName);
    
    return bo;
  }

}
